﻿<%@ WebHandler Language="C#" Class="hesuan_pandian_shuini" %>

using System;
using System.Web;
using KeaoBase;
using ServiceStack.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Data;

public class hesuan_pandian_shuini : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            if (KeaoRedis.ContainsKey(key))
            {
                context.Response.Write(KeaoRedis.ReadString(key));
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string requestType = string.Format("{0}", context.Request.QueryString["requestType"]);
                switch (requestType)
                {
                    case "1":
                        string kaishi = string.Format("{0}", context.Request.QueryString["kaishi"]);
                        string jieshu = string.Format("{0}", context.Request.QueryString["jieshu"]);
                        string zhandian = string.Format("{0}", context.Request.QueryString["zhandian"]);
                        object[] SqlParams1 = new object[] {
                               m_DB.CreateParamter("@kaishishijian",string.Format("{0} 00:00:00",kaishi),false),
                               m_DB.CreateParamter("@jieshushijian",string.Format("{0} 23:59:59",jieshu),false),
                               m_DB.CreateParamter("@zhandian",string.Format("{0}",zhandian),false)
                        };
                        DataSet  Ds1 = m_DB.ExecuteTableProcedure("P_HeSuan_ShuiNi", SqlParams1);
                        if (Ds1.Tables.Count > 0 && Ds1.Tables[0].Rows.Count > 0)
                        {
                            DataRow newRow = Ds1.Tables[0].NewRow();
                            newRow["shuiwenliaoshejiliang"] = Ds1.Tables[1].Rows[0]["shuiwenliaoshejiliang"];
                            newRow["shejixiaohao"] = Ds1.Tables[1].Rows[0]["shejixiaohao"];
                            newRow["pandianxiaohao"] = Ds1.Tables[1].Rows[0]["pandianxiaohao"];
                            newRow["yingkui"] = Ds1.Tables[1].Rows[0]["yingkui"];
                            newRow["yingkuibili"] = Ds1.Tables[1].Rows[0]["yingkuibili"];
                            newRow["shuiwenliaochukuganzhong"]=Ds1.Tables[1].Rows[0]["shuiwenliaochukuganzhong"];
                            newRow["shigongshuinijiliang"]=Ds1.Tables[1].Rows[0]["shigongshuinijiliang"];
                            Ds1.Tables[0].Rows.Add(newRow);
                            string jsonStr = KeaoUtils.Dt2Json(Ds1.Tables[0]);
                            context.Response.Write(jsonStr);
                            KeaoRedis.WriteString(key, jsonStr, 600);
                        }
                        else
                        {
                            context.Response.Write("[]");
                        }
                        break;
                    case "2":
                        string hesuanid = string.Format("{0}", context.Request.QueryString["hesuanid"]);
                        object[] createParams = new object[] {
                            m_DB.CreateParamter("@hesuanID",hesuanid,false)
                        };
                        DataSet biaoDuanDt=m_DB.ExecuteTableProcedure("P_HeSuanShiGongXinXiByHeSuanID", createParams);
                        if (biaoDuanDt.Tables.Count > 0)
                        {
                            string biaoDuanJsonStr = KeaoUtils.Dt2Json(biaoDuanDt.Tables[0]);
                            context.Response.Write(biaoDuanJsonStr);
                        }
                        break;
                    case "3":
                        string kaipanriqi = string.Format("{0} 00:00:00", context.Request.QueryString["kaipanriqi"]);
                        string peibizhandian = string.Format("{0}", context.Request.QueryString["zhandian"]);
                        string peibigongcheng = string.Format("{0}", context.Request.QueryString["gongcheng"]);
                        string peibiCondition = m_DB.CreateAndCondition("peibiriqi", DataValueCommonType.日期时间, kaipanriqi, "", SearchConditionType.等于);
                        peibiCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, peibizhandian, "", SearchConditionType.等于);
                        peibiCondition += m_DB.CreateAndCondition("gongcheng", DataValueCommonType.字符串, peibigongcheng, "", SearchConditionType.等于);
                        Dictionary<string, string> peibiOrderbys = new Dictionary<string, string>();
                        peibiOrderbys.Add("guanlianwuliao", "asc");
                        DataTable peibiDt = m_DB.GetDataTableByCondition("tbl_peihebi", peibiCondition,peibiOrderbys);
                        string peibiJsonStr = KeaoUtils.Dt2Json(peibiDt);
                        context.Response.Write(peibiJsonStr);
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("盘点水泥核算异常。#{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}